import 'package:dali_ng_pera/page/certification/dialog/dnp_time_choice_dialog.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ios_keyboard_action/ios_keyboard_action.dart';

import '../controller/dnp_refresh_controller.dart';
import '../model/dnp_identity_upload_model.dart';

class DnpIdentityPleaseConfirmDialog extends StatelessWidget {
  final controller = Get.put(DnpRefreshController());
  final VoidCallback? onConfirm;
  final VoidCallback? onCancel;
  final DnpIdentityUploadModel model;

  // 为可编辑字段添加 TextEditingController
  final TextEditingController nameController = TextEditingController();
  final TextEditingController idNoController = TextEditingController();

  DnpIdentityPleaseConfirmDialog(
      {super.key, this.onCancel, this.onConfirm, required this.model}) {
    // 初始化控制器的值
    nameController.text = model.beersheba ?? "";
    idNoController.text = model.suburb ?? "";
  }

  String get getKey => this.hashCode.toString();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Center(
        child: Container(
          width: 309,
          height: 488,
          padding: const EdgeInsets.all(14),
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/ic_identity_correct_bg.webp'),
              fit: BoxFit.fill,
            ),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(height: 65),
              Container(
                alignment: Alignment.center,
                child: Text(
                  'Please confirm',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                    color: Color(0xFF252630),
                  ),
                ),
              ),
              const SizedBox(height: 6),
              _buildTextField(FocusNode(),context, "Name", model.beersheba ?? "",
                  canEdit: true, controller: nameController),
              _buildTextField(FocusNode(),context, "ID No.", model.suburb ?? "",
                  canEdit: true, controller: idNoController),
              GetBuilder<DnpRefreshController>(
                  id: getKey,
                  builder: (controller) {
                    return _buildTextField(FocusNode(),context, "Date Birth",
                        getDateBirth(),
                        canClick: true);
                  }),
              const SizedBox(
                height: 12,
              ),
              Text(
                "*Please check your lD information correctly, oncesubmitted it is not changed again",
                style: TextStyle(
                  fontSize: 12,
                  fontWeight: FontWeight.w400,
                  color: Color(0xFFF14343),
                ),
              ),
              const SizedBox(
                height: 14,
              ),
              Row(
                children: [
                  Expanded(
                      child: GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    onTap: () {
                      onCancel?.call();
                      Get.back();
                    },
                    child: Container(
                      width: double.infinity,
                      height: 48,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: Color(0xFFCFE6FF),
                        borderRadius: BorderRadius.circular(16),
                      ),
                      child: Text(
                        "Back",
                        style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.w600,
                          color: Color(0xFF95A5B9),
                        ),
                      ),
                    ),
                  )),
                  const SizedBox(
                    width: 9,
                  ),
                  Expanded(
                      child: GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    onTap: () {
                      // 保存用户输入的值
                      model.beersheba = nameController.text;
                      model.suburb = idNoController.text;
                      onConfirm?.call();
                    },
                    child: Container(
                      width: double.infinity,
                      height: 48,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: Color(0xFF1D4AC8),
                        borderRadius: BorderRadius.circular(16),
                      ),
                      child: Text(
                        "Confirm",
                        style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.w600,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  )),
                  const SizedBox(
                    width: 13,
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildTextField(FocusNode focusNode,BuildContext context, String title, String content,
      {bool canClick = false,
      bool canEdit = false,
      TextEditingController? controller}) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const SizedBox(height: 8),
        Text(
          title,
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.w600,
            color: Color(0xFF252630),
          ),
        ),
        const SizedBox(height: 7),
        GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: canClick
              ? () {
                  showDialog(
                      context: context,
                      builder: (context) {
                        return DnpTimeChoiceDialog(
                          curTime:
                              "${model.worshipper}-${model.business}-${model.ceremonial}",
                          onConfirm: (time) {
                            model.business =
                                time.day.toString().padLeft(2, "0");
                            model.worshipper =
                                time.month.toString().padLeft(2, "0");
                            model.ceremonial = time.year.toString();
                            this.controller.update([getKey]);
                          },
                        );
                      });
                }
              : null,
          child: Container(
            width: double.infinity,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(16),
              color: Color(0xFFF1F1F2),
            ),
            margin: const EdgeInsets.only(right: 13),
            padding:
                const EdgeInsets.only(left: 16, right: 16, top: 12, bottom: 13),
            child: canEdit
                ? IOSKeyboardAction(
                  focusNode: focusNode,
                  backgroundColor:Colors.grey.shade300,
                  label: "Done",
                  textColor: Colors.blue,
                  focusActionType: FocusActionType.done,
                  onTap: (){
                    FocusScope.of(context).requestFocus(FocusNode());
                  },
                  child: TextField(
                      focusNode: focusNode,
                      controller: controller,
                      style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.w600,
                          color: Color(0xFF555555)),
                      decoration: InputDecoration(
                        border: InputBorder.none,
                        isDense: true,
                        contentPadding: EdgeInsets.zero,
                      ),
                    ),
                )
                : Text(
                    content,
                    style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                        color: Color(0xFF555555)),
                  ),
          ),
        ),
      ],
    );
  }

  String getDateBirth() {
    if (model.ceremonial != null &&
        model.ceremonial!.isNotEmpty &&
        model.worshipper != null &&
        model.worshipper!.isNotEmpty &&
        model.business != null &&
        model.business!.isNotEmpty) {
      return "${model.ceremonial ?? ""}-${(model.worshipper ?? "").padLeft(2, "0")}-${(model.business ?? "").padLeft(2, "0")}";
    }
    return "please select";
  }
}
